import * as path from 'path'
import * as os from 'os'
import * as fs from 'fs'
import { Loggor } from './../loggor'
export class Configrate {
  private static dbPath: string
  private static dbRoot: string
  private static config: DBConfig
  static getConfig() {
    if (!this.config) {
      this.dbPath = path.join(os.homedir(), '.pfms')
      this.dbRoot = path.join(this.dbPath, '.data.db')
      this.checkDataFile()
      this.config = new DBConfig()
      this.config.dbname = 'Mf2GGqyCsuUU'
      this.config.uname = 'u5IPDfneHLOL'
      this.config.upwd = '788Cijv8esqp'
      this.config.dialect = 'sqlite'
      this.config.storage = this.dbRoot
      const define = new DBDefinetion()
      define.underscored = true
      define.timestamps = true
      define.paranoid = true
      this.config.define = define
    }
    return this.config
  }
  private static checkDataFile(): void {
    try {
      fs.accessSync(this.dbPath, fs.constants.R_OK | fs.constants.W_OK)
      Loggor.log('data file found.')
    } catch (err) {
      Loggor.error(err)
      try {
        fs.mkdirSync(this.dbPath)
        try {
          fs.writeFileSync(this.dbRoot, '')
          Loggor.log('data file created.')
        } catch (e) {
          Loggor.error(e)
        }
      } catch (er) {
        Loggor.error(er)
      }
    }
  }
}

export class DBConfig {
  dbname: string
  uname: string
  upwd: string
  dialect: string
  storage: string
  host: string
  define: DBDefinetion
}
class DBDefinetion {
  underscored: boolean
  timestamps: boolean
  paranoid: boolean
}
